/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.das;

import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.portalcenter.dto.CaptchaDTO;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.portalcenter.application.bo.AuthBO;
import cn.hallele.ms.portalcenter.application.bo.CaptchaBO;
import cn.hallele.ms.starter.web.framework.ActionExecutor;
import cn.hallele.ms.starter.web.framework.context.ActionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/10/6 16:12
 */
@Slf4j
@Service
public class AuthDBDas extends ModelDas<ActionExecutor, Void, Void> {
    protected AuthDBDas(ActionExecutor option) {
        super(option);
    }

    private static final String CREATE_CAPTCHA = "createCaptchaApplicationService";
    private static final String LOGIN = "loginApplicationService";
    private static final String LOGOUT = "logoutApplicationService";

    /**
     * 登录接口
     *
     * @param username    用户名
     * @param password    密码
     * @param captchaKey  验证码key
     * @param captchaCode 验证码值
     */
    public void login(String username, String password, String captchaKey, String captchaCode) {
        AuthBO authBO = AuthBO.builder()
                .username(username)
                .password(password)
                .captchaKey(captchaKey)
                .captchaCode(captchaCode)
                .build();
        this.option.execute2Resp(LOGIN, authBO, new ActionContext());
    }

    /**
     * 注销登录
     */
    public void logout() {
        this.option.execute2Resp(LOGOUT, null, new ActionContext());
    }

    /**
     * 生成验证码
     *
     * @return 验证码对象
     */
    public CaptchaDTO createCaptcha() {
        ActionContext context = new ActionContext();
        this.option.execute2Resp(CREATE_CAPTCHA, null, context);
        CaptchaBO result = context.getResult();
        return revert(result);
    }

    /**
     * 将T转成E
     *
     * @param unused 被转换者
     * @return S 转换后者
     */
    @Override
    protected Void convert(Void unused) {
        return null;
    }

    /**
     * 将E回转陈T
     *
     * @param unused 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected Void revert(Void unused) {
        return null;
    }

    /**
     * 将E回转陈T
     *
     * @param captchaBO 被回转换者
     * @return S 回转换后者
     */
    protected CaptchaDTO revert(CaptchaBO captchaBO) {
        return ObjectUtil.map(captchaBO, c -> CaptchaDTO.builder()
                .key(c.getKey())
                .captcha(c.getCaptcha())
                .build());
    }
}
